package com.eims.sls_app.ui;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.OnClick;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.MySingleton;
import bls.juejin.cn.R;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.RequestUtil;
import com.eims.sls_app.view.MyToast;

public class AboutAppActivity extends MyBaseActivity {
	private static final String TAG = "AboutAppActivity";
	private Context context = AboutAppActivity.this;

	// @InjectView(R.id.iv_about)
	// 图片
	ImageView iv_about;

	// @InjectView(R.id.btn_back)
	// 返回键
	ImageButton btn_back;

	// @InjectView(R.id.tv_about_version)
	// 版本信息
	TextView tv_about_version;

	// @InjectView(R.id.tv_about_company)
	// 公司名称
	TextView tv_about_company;

	// @InjectView(R.id.tv_about_phone_name)
	// 站点名称/电话
	TextView tv_about_phone_name;

	// @InjectView(R.id.tv_about_website)
	// 公司网站
	TextView tv_about_website;

	private String imgUrl;
	private String URL = "m.haoqiucai.com";
	private String appVersion;
	private String companyName;
	private String servicePhone;
	private String siteName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_aboutapp);
		// ButterKnife.inject(this);
		getSiteNameAndPhone();
		// 初始化控件
				initView();
	}

	private void init() {
		
		if (!"".equals(URL)) {
			String htmlLinkText = "<a href=\"/\" mce_href=\"/\" \"\"><u>" + URL
					+ "</u></a>";
			tv_about_website.setText(Html.fromHtml(htmlLinkText));
			tv_about_website
					.setMovementMethod(LinkMovementMethod.getInstance());
			CharSequence text = tv_about_website.getText();
			if (text instanceof Spannable) {
				int end = text.length();
				Spannable sp = (Spannable) tv_about_website.getText();
				URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
				SpannableStringBuilder style = new SpannableStringBuilder(text);
				style.clearSpans();// should clear old spans
				for (URLSpan url : urls) {
					MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
					style.setSpan(myURLSpan, sp.getSpanStart(url),
							sp.getSpanEnd(url),
							Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				}
				tv_about_website.setText(style);
			}
		}
		appVersion = App.version;
		tv_about_version.setText("当前版本：" + appVersion);
		if (!"".equals(siteName) && !"".equals(servicePhone)) {
			tv_about_phone_name.setText("【" + siteName + "】");
		}
		if (!"".equals(companyName)) {
			tv_about_company.setText("版权所有：" + companyName);
		}
		/*if (!"".equals(imgUrl)) {
			ImageLoader mImgLoader = MySingleton.getInstance(context)
					.getImageLoader();
			ImageLoader.ImageListener listener = ImageLoader.getImageListener(
					iv_about, R.drawable.init_lottery,
					R.drawable.image_download_failure);
			mImgLoader.get(imgUrl, listener);
		}*/
	}

	private void initView() {
		// 图片
		iv_about = (ImageView) findViewById(R.id.iv_about);

		// 返回键
		btn_back = (ImageButton) findViewById(R.id.btn_back);

		// 版本信息
		tv_about_version = (TextView) findViewById(R.id.tv_about_version);

		// 公司名称
		tv_about_company = (TextView) findViewById(R.id.tv_about_company);

		// 站点名称/电话
		tv_about_phone_name = (TextView) findViewById(R.id.tv_about_phone_name);

		// 公司网站
		tv_about_website = (TextView) findViewById(R.id.tv_about_website);

	}

	/**
	 * 获取站点名称和客服电话
	 */
	public void getSiteNameAndPhone() {
		RequestUtil requestUtil = new RequestUtil(AboutAppActivity.this, false,
				0, false, "正在加载信息...") {
			@Override
			public void responseCallback(JSONObject jsonObject) {
				if (RequestUtil.DEBUG)
					Log.i(TAG,
							"获取站点名称和客服电话jsonObject--- " + jsonObject.toString());
				String name = jsonObject.optString("SiteName");
				String phone = jsonObject.optString("Phone");
				String company = jsonObject.optString("CompanyName");
				String url = jsonObject.optString("SiteURL");
				String img = jsonObject.optString("SiteImg");
				if (!"".equals(name)) {
					App.APP_NAME = name;
					siteName = name;
				}
				if (!"".equals(phone)) {
					App.SERVICE_PHONE = phone;
					servicePhone = phone;
				}
				if (!"".equals(company)) {
					companyName = company;
				}
				if (!"".equals(url)) {
//					URL = url;
				}
				if (!"".equals(img)) {
					imgUrl = img;
				}
				init();
			}

			@Override
			public void responseError(VolleyError error) {
				if (RequestUtil.DEBUG)
					Log.e(TAG, "获取站点名称和客服电话出错--- " + error.getMessage());
				MyToast.getToast(context, "服务器错误，请重试。").show();
				init();
				AboutAppActivity.this.finish();
			}
		};
		requestUtil.getSiteNameAndPhone();
	}

	// @OnClick(R.id.btn_back)
	// public void back() {
	// this.finish();
	// }

	public void back(View view) {
		this.finish();
	}

	private class MyURLSpan extends ClickableSpan {

		private String mUrl;

		MyURLSpan(String url) {
			mUrl = url;
		}

		@Override
		public void onClick(View widget) {
			Intent intent = new Intent();
			intent.setAction("android.intent.action.VIEW");
			Uri content_url = Uri.parse("http://"+URL);
			intent.setData(content_url);
			AboutAppActivity.this.startActivity(intent);
		}
	}
}
